package com.egao.common.publicly;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.egao.common.core.utils.ExcelUtils;
import com.egao.common.core.utils.StudentSignlogUtil;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.student.entity.StudentSign;
import com.egao.common.module.student.entity.vo.StudentSignAdminListVO;
import com.egao.common.module.student.service.DispatchCompanyService;
import com.egao.common.module.student.service.StudentSignService;
import com.egao.common.publicly.entity.ArticleVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;

/**
 * @author lym
 * @date 2024/6/17 19:15
 */
@Api(tags = "签约")
@RestController
@RequestMapping("${api}")
public class PubliclySignController {

    @Autowired
    private StudentSignService studentSignService;
    @Autowired
    private DispatchCompanyService dispatchCompanyService;

    @Value("${log.path}")
    private String logPath;

    @GetMapping("/publicly/sign/check")
    public void check(HttpServletResponse response, String brluhqam) {
        if (!"YV0VKrQx62C4g7vSITi2".equals(brluhqam)) {
            return;
        }

        List<StudentSign> studentSignList = studentSignService.tempList();


        List<Map<String, String>> list = new ArrayList<>();
        for (StudentSign item : studentSignList) {
            String oldDispatchReportCompanyProvince = item.getDispatchReportCompanyProvince();
            String oldDispatchReportCompanyAddress = item.getDispatchReportCompanyAddress();
            String oldDispatchCompanyMembership = item.getDispatchCompanyMembership();
            dispatchCompanyService.getDispatchByCode(item, item.getSyszd());
            String newDispatchReportCompanyProvince = item.getDispatchReportCompanyProvince();
            String newDispatchReportCompanyAddress = item.getDispatchReportCompanyAddress();
            String newDispatchCompanyMembership = item.getDispatchCompanyMembership();

            Map<String, String> map = new HashMap<>();
            map.put("id", item.getId() + "");

            map.put("p1", oldDispatchReportCompanyProvince);
            map.put("p2", newDispatchReportCompanyProvince);
            map.put("p3", !StrUtil.isBlank(oldDispatchReportCompanyProvince) && oldDispatchReportCompanyProvince.equals(newDispatchReportCompanyProvince) ? "相同" : "不相同");

            map.put("a1", oldDispatchReportCompanyAddress);
            map.put("a2", newDispatchReportCompanyAddress);
            map.put("a3", !StrUtil.isBlank(oldDispatchReportCompanyAddress) && oldDispatchReportCompanyAddress.equals(newDispatchReportCompanyAddress) ? "相同" : "不相同");

            map.put("m1", oldDispatchCompanyMembership);
            map.put("m2", newDispatchCompanyMembership);
            map.put("m3", !StrUtil.isBlank(oldDispatchCompanyMembership) && oldDispatchCompanyMembership.equals(newDispatchCompanyMembership) ? "相同" : "不相同");

            list.add(map);
        }



        // 导出excel模板
        ExcelWriter writer = ExcelUtil.getWriter(true);

        // 设置别名和排序
        excelAddHeaderAlias(writer);

        // 写出内容 xlsx
        writer.write(list, true);

        // 设置列自适应宽 必须在write以后
        ExcelUtils.autoColumnMaxSizeAll(writer.getSheet());

        String fileName = "student-sign-check-" + DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN) + ".xlsx";

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);

        try {
            OutputStream out = response.getOutputStream();
            writer.flush(out, true);
            writer.close();
            IoUtil.close(out);
        } catch (IOException e) {
            writer.close();
        } finally {
            writer.close();
        }
    }

    public void excelAddHeaderAlias(ExcelWriter writer) {
        writer.addHeaderAlias("id", "主键");
        writer.addHeaderAlias("p1", "p1");
        writer.addHeaderAlias("p2", "p2");
        writer.addHeaderAlias("p3", "p3");
        writer.addHeaderAlias("a1", "a1");
        writer.addHeaderAlias("a2", "a2");
        writer.addHeaderAlias("a3", "a3");
        writer.addHeaderAlias("m1", "m1");
        writer.addHeaderAlias("m2", "m2");
        writer.addHeaderAlias("m3", "m3");
        // 只显示 writer.addHeaderAlias 的字段
        writer.setOnlyAlias(true);
    }
}
